package zdream.pmfield.stage.buff.foe;

import java.util.Objects;
import java.util.function.Function;

import zdream.pmfield.data.foe.Ability;
import zdream.pmfield.data.foe.MoveAdditionalEffects;
import zdream.pmfield.stage.director.HookStore;
import zdream.pmfield.stage.director.HookType;
import zdream.pmfield.stage.director.Hooks;
import zdream.pmfield.stage.effect.AbstractEffect;
import zdream.pmfield.stage.effect.Effects;
import zdream.pmfield.stage.object.FoeActor;

/**
 * 战斗开始时, 或者 {@link zdream.pmfield.stage.object.FoeActor} 刚上场时触发,
 * 用 {@link #ability} 创建完对应的 {@link zdream.pmfield.stage.buff.StageBuff}
 * 之后, 就删掉这个 {@link AbilityExecutingBuff}
 *
 * @author Zdream
 * @date 2023-12-17
 * @since 0.0.1
 */
public class AbilityExecutingBuff extends FoeStageBuff {
	public static final String CATEGORY = "abilityExecuting";
	public final Ability ability;

	public AbilityExecutingBuff(FoeActor target, Ability ability) {
		super(target);
		this.ability = Objects.requireNonNull(ability);
	}

	@Override
	public void init() {
		putAbilityBuff();
	}

	public void putAbilityBuff() {
		final Effects effects = target.stage.director.effects;
		// 特性的效果都默认延后触发
		Function<HookType, HookStore.HookHandlerBuilder> onHook = type -> this.onHook(type).withPriority(100);

		for (MoveAdditionalEffects.EffectDescription description : ability.createDescriptions()) {
			AbstractEffect effect = effects.createEffect(description);
			effect.init(onHook);
		}

		// 现在触发
		final Hooks.FoeBuffContext context = target.triggerHook(HookType.ON_CREATE_BUFF_BY_ABILITY).as();
		context.withFoe(target).run();
	}

	@Override
	public String getCategory() {
		return CATEGORY;
	}

	@Override
	public String toString() {
		return "创建特性:" + ability.name;
	}
}
